//获取应用实例
var app = getApp()
var promise = require('../../utils/request.js');
var util = require('../../utils/util.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    num: 1,
    minusStatus: 'disabled',
    laterUserNumber: 1,
    id: '',
    type: '',
    price: ''
  },
  /* 点击减号 */
  bindMinus: function () {
    var num = this.data.num;
    // 如果大于1时，才可以减 
    if (num > 1) {
      num--;
    }
    // 只有大于一件的时候，才能normal状态，否则disable状态 
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // 将数值与状态写回 
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },
  /* 点击加号 */
  bindPlus: function () {
    var num = this.data.num;
    // 不作过多考虑自增1 
    num++;
    // 只有大于一件的时候，才能normal状态，否则disable状态 
    var minusStatus = num < 1 ? 'disabled' : 'normal';
    // 将数值与状态写回 
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },
  /* 输入框事件 */
  bindManual: function (e) {
    var num = e.detail.value;
    // 将数值与状态写回 
    this.setData({
      num: num
    });
  },
  onLoad: function (options) {
    var that = this;
    var param = {}
    param.id = options.id,
    param.type = options.type,
    promise.sendRequest('account/getAccountPrice', 'post', param, null, 4, {
      success: function (data) {
        that.data.id = options.id,
        that.data.type = options.type,
        that.data.price = data.price,
        that.setData(data);
      }
    }, {
        fail: function (res) {
          console.log(res)
        }
      })

  },
  saveApply: function () {
    var that = this;
    var param = {}
    param.id = that.data.id,
    param.type = that.data.type,
    param.laterUserNumber = that.data.num,
    param.price = that.data.price;
    promise.sendRequest('account/saveApply', 'post', param, null, 4, {
      success: function (data) {
        console.log(data)
        if (data) {
          that.charge(data);
        }
      }
    }, {
        fail: function (res) {
          console.log(res)
          promise.toastError(res)
        }
      })

  },

  charge: function (bill) {
    let that=this
    var payInfo = {}
    payInfo.unionId = wx.getStorageSync('unionId'),
      payInfo.openId = wx.getStorageSync('openId'),
    payInfo.laterUserNumber = this.data.num,
    payInfo.quantity = 1,
    payInfo.unitPrice = 600,
    payInfo.accountSuite = this.data.code,
    payInfo.name = '账套加点',
      payInfo.bill = bill,
    promise.sendRequest('miniapp/pay', 'Post', payInfo, null, 4, {
      success: function (data) {
        console.log(data)
        if (data) {
          wx.requestPayment({
            'timeStamp': data.timeStamp,
            'nonceStr': data.nonceStr,
            'package': data.packageStr,
            'signType': data.signType,
            'paySign': data.paySign,
            'success': function (res) {
                console.log(res)
                wx.redirectTo({
                  url: '../RechargeState/RechargeState?state=' + 1 + '&id=' + that.data.id
                })
            },
            'fail': function (res) {
                console.log(res)
                wx.showToast({
                  title: "支付失败！",
                  icon: 'none',
                  duration: 2000
                })
              wx.redirectTo({
                  url: '../RechargeState/RechargeState?state=' + 2 + '&id=' + that.data.id
                })
            }
          })
        }
      }
    }, {
        fail: function (res) {
          console.log(res)
        }
      })
  }
 
})